import requests
from bs4 import BeautifulSoup
import re
def html_parser(url):
    '''打开传进来的URL，打开URL后转成beautiful对象并返回'''
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)  Chrome/65.0.3325.181 Safari/537.36",
        "Host": "quote.eastmoney.com",
    }

    r = requests.get(url, headers=headers)

    # 该网站的charset是gb2312，所以要用gb2312解码
    html = BeautifulSoup(r.content.decode("gb2312","ignore"), "html.parser")

    return html

def get_stock_list(url):
    '''打开该网站的主页，得到所有股票名称和其详情页的URL'''
    html = html_parser(url)

    links = html.select("a")

    stock_list = [ ]

    for link in links:
        name_code = link.text
        if "(" in name_code:
            split_name_code = name_code.split("(")
            name = split_name_code[0]
            code = split_name_code[1].replace(")","")
            stock_list.append(dict(name=name,code=code))

    return stock_list

def get_stock_info(stock):
    '''股票详情页是通过js异步加载的，已得知其请求方式，模拟请求'''
    
    stock_info = None

    stock_info = request_data(stock,1)
    if stock_info:
        return stock_info
    else:
        stock_info = request_data(stock,2)
        return stock_info
            

def request_data(stock,market):
    token = "4f1862fc3b5e77c150a2b985b12db0fd"
    code = stock.get("code")
    name = stock.get("name")
    if len(code) != 6:
        return
    api = f"http://nuff.eastmoney.com/EM_Finance2015TradeInterface/JS.ashx?id={code}{market}&token=4f1862fc3b5e77c150a2b985b12db0fd&cb=jQuery18303523167263317595_1528391320429&_=1528391320472"
    
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)  Chrome/65.0.3325.181 Safari/537.36",
        "Host": "nuff.eastmoney.com",
    }
    r = requests.get(api, headers=headers)
    parse = re.findall(r'{.*?}',r.text)
    
    try:
        parse = parse[0].replace("null",'"无"')
        
        values = eval(parse).get("Value")

        if values:
            stock_info = dict(
                时间 = values[49],
                今开 = values[28],
                昨收 = values[34],
                最高 = values[30],
                最低 = values[32],
                涨停 = values[23],
                跌停 = values[24],
                换手 = values[37] + "%",
                量比 = values[36],
                成交量 = values[31],
                成交额 = values[35],
                市盈 = values[38],
                市净 = values[43],
                总市值 = values[46],
                流通市值 = values[46]
            )
            return stock_info

    except Exception as e:
        print(e)


        
def main():
    stock_list = get_stock_list("http://quote.eastmoney.com/stocklist.html")
    final_list = [ ]
    for stock in stock_list:
        info = get_stock_info(stock)
        if info:
            stock = stock
            stock['info'] = info
            print(stock)
            final_list.append(stock)

    with open("stock.txt","w",encoding="utf-8") as f:
        f.write(str(final_list))

if __name__ == "__main__":
    main()

